﻿using MediatR;
using PublicCommon;
using T_Rbac.Read.APi.Application.Command.Jobs;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac.Read.APi.Application.Handlers.JobsHandlers
{
    public class JobSearchByPageresultHandlers : IRequestHandler<JobSearchByPageResult, PageResult<Job>>
    {
        private readonly IjobRep ijobRep;

        public JobSearchByPageresultHandlers(IjobRep ijobRep)
        {
            this.ijobRep = ijobRep;
        }

        public async Task<PageResult<Job>> Handle(JobSearchByPageResult request, CancellationToken cancellationToken)
        {
            var list = await ijobRep.GetvaluesByResult(x => !x.IsDel);
            if (!string.IsNullOrEmpty(request.jname))
            {
                list = list.Where(x => x.Jname == request.jname);
            }
            int Listcount = list.Count();
            int Pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pagesize);
            list = list.OrderByDescending(x => x.Jid).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize);
            return new PageResult<Job>
            {
                List = list.ToList(),
                PageCount = Pagecount,
                TotalCount = Listcount
            };
        }
    }
}
